<?php
/**
* OpenID module
* For Exponent 2.0pr2
* @author: trungdt
* @version: 1.0
* @since: Jan 21, 2011
* 
* Change logs
* Jan 29, 2011 - trungdt - implement function Default shown in homepage
* jan 30, 2011 - trungdt - support ajax im chat auto login
* Feb 9, 2011 - trungdt - Add permission of demand module into openid
*/
class openidmodule {
	function name() { return "OpenID Module"; }
	function description() { return "OpenID module for Exponent"; }
	function author() { return 'ABSoft'; }
	
	function hasContent() { return false; }
	function hasSources() { return false; }
	function hasViews()   { return false; }
	
	function supportsWorkflow() { return false; }
	
	function permissions($internal = '') {
	   return array(   
            'administrate'=>'Administrate',
            'view_userprofile'=>'View User Profile',
            'edit_userprofile'=>'Edit User Profile',
            'remove_userprofile'=>'Remove User Profile'
        );
	}
	
	function deleteIn($loc) {
		// Do nothing, no content
	}
	
	function copyContent($from_loc,$to_loc) {
		// Do nothing, no content
	}
	
	function show($view,$loc = null, $title = '') {
        global $user, $db;
        
        // check this user is logged by openid or by current account
		$openid_login=aboid_isLoggedin();
        $exp_login=exponent_users_isLoggedIn();
        
        $view="Default";
        $data=null;
        if ($exp_login)
        {
            // show user info
            $data=$user;
            if (($data->firstname=="") && ($data->lastname==""))
            {
                $data->firstname="Unknown";
            }
        }
        else if ($openid_login) 
        {
            // show open ID info, then show create account button
            $view="_DefaultOpenID";
            $data=aboid_getSession();
        }
        else
        {
            return;
        }
        $template = new template('openidmodule',$view,$loc);
        $template->assign("data",$data);
        // Feb 9, 2011 - trungdt -- force add permission of demandmodule into openidmodule - begin
        $myloc->mod="demandmodule";
        $template->register_permissions(array('approve_demand',"administrate"),$myloc);  
        // Feb 9, 2011 - trungdt -- force add permission of demandmodule into openidmodule - begin
        
        // jan 30, 2011 - trungdt - auto login ajaxim chat - begin
        // find chat id
        /*if (exponent_users_isLoggedIn())
        {
            $temp=$db->selectObject("chat_users","username = '{$user->username}'");
            if ($temp){
                $_SESSION['username'] = $user->username;
                $_SESSION['user_id'] = intval($temp->user_id);
                $temp->last_login=date("Y-m-d h:i:s");
                $db->sql("UPDATE {$db->prefix}chat_users SET last_login='{$temp->last_login}' WHERE user_id = {$temp->user_id}");
            }
            else
            {
                // insert this user into chat user tables
                $ip=ip2long($_SERVER['REMOTE_ADDR']);
                $cuser=$user->username;
                $clast_login=date("Y-m-d h:i:s");
                $sql="INSERT INTO {$db->prefix}chat_users(username, last_known_ip , last_login) VALUES ('{$cuser}', {$ip}, '{$clast_login}')";
                $db->sql($sql);
                $temp=$db->selectObject("chat_users","username = '{$cuser}'");
                $_SESSION['username'] = $user->username;
                $_SESSION['user_id'] = intval($temp->user_id);
            }
            $json='{sid:"'.session_id().'",user:"'.$user->username.'"}';
            $template->assign("chat_session",$json);
        }*/
        // jan 30, 2011 - trungdt - auto login ajaxim chat - end
        
		$template->output($view);
	}
	
	function spiderContent($item = null) {
		// Do nothing, no content
		return false;
	}
	
}